﻿Imports System.Data
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports TepcelDominio
Imports TepcelDAL.DBManager
Public Class AlmacenDAL : Inherits DBManager
    Public Function NuevoAlmacen(ByVal almacen As Almacen) As String
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("SPInsertAlmacen")

        comando.Parameters.Add("@codigoAlmacen", SqlDbType.Char, 4).Value = almacen.codigoAlmacen
        comando.Parameters.Add("@nombreAlmacen", SqlDbType.VarChar, 60).Value = almacen.nombreAlmacen
        comando.Parameters.Add("@telefonoAlmacen", SqlDbType.VarChar, 10).Value = almacen.telefonoAlmacen
        comando.Parameters.Add("@codigoUbigeo", SqlDbType.Char, 5).Value = almacen.codigoUbigeo
        comando.Parameters.Add("@serieAlmacen", SqlDbType.Char, 3).Value = almacen.serieAlmacen

        comando.Parameters("@codigoAlmacen").Direction = ParameterDirection.InputOutput

        MyBase.ExecuteQuery(comando)
        almacen.codigoAlmacen = comando.Parameters("@codigoAlmacen").Value

        Return almacen.codigoAlmacen

    End Function

    Public Function GetAllFromAlmacen() As List(Of Almacen)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("GetAllFromAlmacen")
        Return SelectObjectFactory(comando)
    End Function

    Public Function SearchFromAlmacen(ByVal texto As String) As List(Of Almacen)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("searchAlmacen")

        comando.Parameters.Add("@texto", SqlDbType.VarChar, 200).Value = texto

        Return SelectObjectFactory(comando)

    End Function

    Public Function DeleteAlmacen(ByVal codigoAlmacen As String) As Boolean
        Dim correcto As Boolean = False

        Dim comando As SqlCommand = MyBase.CreateSelectCommand("DeleteAlmacen")

        comando.Parameters.Add("@codigoAlmacen", SqlDbType.Char, 4).Value = codigoAlmacen

        If MyBase.ExecuteQuery(comando) > 0 Then
            correcto = True
        End If

        Return correcto
    End Function

    Private Function SelectObjectFactory(ByVal comando As SqlCommand) As List(Of Almacen)
        Dim Lista As New List(Of Almacen)
        Using reader As SqlDataReader = MyBase.ExecuteReader(comando)
            While reader.Read
                Dim Almacen As New Almacen(reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4))
                Lista.Add(Almacen)
                Almacen = Nothing
            End While
        End Using
        Return Lista
    End Function

End Class
